package cn.iocoder.yudao.module.product.controller.admin.calculationmodel;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.module.product.controller.admin.calculationmodel.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.calculationmodel.CalculationModelDO;
import cn.iocoder.yudao.module.product.convert.calculationmodel.CalculationModelConvert;
import cn.iocoder.yudao.module.product.service.calculationmodel.CalculationModelService;

@Tag(name =  "管理后台 - 核算模型")
@RestController
@RequestMapping("/product/calculation-model")
@Validated
public class CalculationModelController {

    @Resource
    private CalculationModelService calculationModelService;

    @PostMapping("/create")
    @Operation(summary = "创建核算模型")
    @PreAuthorize("@ss.hasPermission('product:calculation-model:create')")
    public CommonResult<Long> createCalculationModel(@Valid @RequestBody CalculationModelCreateReqVO createReqVO) {
        return success(calculationModelService.createCalculationModel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新核算模型")
    @PreAuthorize("@ss.hasPermission('product:calculation-model:update')")
    public CommonResult<Boolean> updateCalculationModel(@Valid @RequestBody CalculationModelUpdateReqVO updateReqVO) {
        calculationModelService.updateCalculationModel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除核算模型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:calculation-model:delete')")
    public CommonResult<Boolean> deleteCalculationModel(@RequestParam("id") Long id) {
        calculationModelService.deleteCalculationModel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得核算模型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:calculation-model:query')")
    public CommonResult<CalculationModelRespVO> getCalculationModel(@RequestParam("id") Long id) {
        CalculationModelDO calculationModel = calculationModelService.getCalculationModel(id);
        return success(CalculationModelConvert.INSTANCE.convert(calculationModel));
    }

    @GetMapping("/list")
    @Operation(summary = "获得核算模型列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('product:calculation-model:query')")
    public CommonResult<List<CalculationModelRespVO>> getCalculationModelList(@RequestParam("ids") Collection<Long> ids) {
        List<CalculationModelDO> list = calculationModelService.getCalculationModelList(ids);
        return success(CalculationModelConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得核算模型分页")
    @PreAuthorize("@ss.hasPermission('product:calculation-model:query')")
    public CommonResult<PageResult<CalculationModelRespVO>> getCalculationModelPage(@Valid CalculationModelPageReqVO pageVO) {
        PageResult<CalculationModelDO> pageResult = calculationModelService.getCalculationModelPage(pageVO);
        return success(CalculationModelConvert.INSTANCE.convertPage(pageResult));
    }

}
